package com.rhb.af.web.screen.util;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.rhb.af.web.screen.bean.Grid;
import com.rhb.af.web.screen.bean.Option;
import com.rhb.af.web.screen.bean.Screen;
import com.rhb.af.web.screen.bean.Select;

public class ScreenPrase {
	public static Map prase(String filePath) {
		File file = new File(filePath);
		SAXReader reader = new SAXReader();
		Map screens = new HashMap();
		try {
			Document doc = reader.read(file);
			Element root = doc.getRootElement();
			for (Iterator i = root.elementIterator(); i.hasNext();) {
				Element e = (Element) i.next();
				
				Screen screen = null;
				if(e.attributeValue("extend") != null && !"".equals(e.attributeValue("extend"))){
					screen = new Screen((Screen)screens.get(e.attributeValue("extend")));
				}else{
					screen = new Screen();
				}
				
				setAttribute(screen, e);

				setListAttribute(screen.getFields(),"com.rhb.af.web.screen.bean.Field", e.element("fields"));
				setListAttribute(screen.getBehaviors(),"com.rhb.af.web.screen.bean.Behavior", e.element("behaviors"));
				setListAttribute(screen.getFunctions(),"com.rhb.af.web.screen.bean.Function", e.element("functions"));
				setSelectsAttribute(screen.getSelects(),e.element("selects"));
				setGridAttribute(screen, e.element("grids"));
				screens.put(screen.getName(), screen);
			}
			//System.out.println(screens);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return screens;
	}

	private static void setSelectsAttribute(List list, Element element) {
		if (element != null) {
			list.clear();
			for (Iterator ite = element.elementIterator(); ite.hasNext();) {
				Select select;
				select = new Select();
				Element f = (Element) ite.next();
				setAttribute(select, f);
				Iterator i = f.elements().iterator();
				Option option;
				while(i.hasNext()){
					Element e = (Element)i.next();
					option = new Option();
					setAttribute(option,e);
					select.getOptions().add(option);
				}
				//System.out.println("****** " + select.toString());
				list.add(select);
			}
		}
	}

	
	private static  void setListAttribute(List list, String clazz, Element element) {
		if (element != null) {
			list.clear();
			for (Iterator ite = element.elementIterator(); ite.hasNext();) {
				Class c;
				try {
					c = Class.forName(clazz);
					Object obj = c.newInstance();
					Element f = (Element) ite.next();
					setAttribute(obj, f);
					//System.out.println("****** " + obj.toString());
					list.add(obj);
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
	}

	
	private static  void setAttribute(Object obj, Element element) {
		for (Iterator i = element.attributeIterator(); i.hasNext();) {
			Attribute a = (Attribute) i.next();
			try {
				BeanUtils.setProperty(obj, a.getName(), a.getValue());
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
	}

	
	private static  void setGridAttribute(Screen screen, Element element){
		if(element != null){
			screen.getGrids().clear();
			for(Iterator ite = element.elementIterator(); ite.hasNext();){
				Element g = (Element)ite.next();
				Grid grid = new Grid();
				setAttribute(grid,g);
				setListAttribute(grid.getFields(),"com.rhb.af.web.screen.bean.Field",g.element("fields"));
				setListAttribute(grid.getBehaviors(),"com.rhb.af.web.screen.bean.Behavior",g.element("behaviors"));
				screen.getGrids().add(grid);
			}
		}
	}

}
